import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CombinationSumIII0102_216 {

    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {

        backtracking(n,k,1,0);
        return result;
    }

    private void backtracking(int n, int k, int startIndex, int sum) {

        if(sum > n) return;

        if(sum == n && path.size() == k){
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i <= 9 - (k-path.size())+1; i++) {
            path.add(i);
            sum+=i;
            backtracking(n,k,i+1,sum);
            sum-=i;
            path.removeLast();

        }
    }


}
